<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;


class PropMall extends Base
{
    protected $table = 'prop_mall';
    protected $logTable = 'prop_mall_log';
    protected $fields = ['goods_id', 'goods_name', 'goods_type', 'small_img', 'big_img', 'animate_resource', 'is_new', 'animate_resource_small', 'prop_id', 'price', 'msg'];
    private $redisPropMallList = 'PROP:MALL:LIST';
    protected $primaryKey = 'goods_id';
    
    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }
    
    //商品列表
    public function getPropMallListData($goods_type, $page, $page_size)
    {
        $condition = ['goods_type' => $goods_type, 'is_show' => 1];

        $document = DB::table($this->table )
            ->where($condition)
            ->orderBy('sort', 'asc')
            ->offset(($page) * $page_size)
            ->limit($page_size)
            ->get($this->fields);

        $result = self::objToArray($document);

        if (!empty($result)) {
            $imgSource = getPicDomain();
            foreach ($result as $k => $v) {
                if (!empty($v['small_img'])) {
                    $result[$k]['small_img'] = $imgSource . $v['small_img'];
                }
                if (!empty($v['big_img'])) {
                    $result[$k]['big_img'] = $imgSource . $v['big_img'];
                }
                if (!empty($v['animate_resource'])) {
                    $result[$k]['animate_resource'] = $imgSource . $v['animate_resource'];
                }
                if (!empty($v['animate_resource_small'])) {
                    $result[$k]['animate_resource_small'] = $imgSource . $v['animate_resource_small'];
                }
            }
        }

        return $result;
    }


    /**
     * 获取商品信息
     * @param $goods_id
     * @param $is_show
     * @return mixed|null
     */
    public function getPropMallDataById($goods_id, $is_show = 1){
        $condition = ['goods_id' => $goods_id];
        if ($is_show) {
            $condition['is_show'] = $is_show;
        }

        $document = DB::table($this->table )
            ->where($condition)
            ->first($this->fields);

        if (empty($document)){
            return null;
        }

        $result = self::objToArray($document);
        if (!empty($result)) {
            $imgSource = getPicDomain();
            if (!empty($result['small_img'])) {
                $result['small_img'] = $imgSource . $result['small_img'];
            }
            if (!empty($result['big_img'])) {
                $result['big_img'] = $imgSource . $result['big_img'];
            }
            if (!empty($result['animate_resource'])) {
                $result['animate_resource'] = $imgSource . $result['animate_resource'];
            }
            if (!empty($result['animate_resource_small'])) {
                $result['animate_resource_small'] = $imgSource . $result['animate_resource_small'];
            }
        }
        return $result;
    }


    /**
     * 道具商城购买日志记录
     *
     * @param string $type 操作类型 默认 insert
     * @param int $userId 用户ID
     * @param int $toUserId 被赠送人ID
     * @param int $goodsId 商品Id
     * @param int $goodsName 商品名称
     * @param int $count count
     * @param int $price price
     * @return bool
     * */
    public function insertPropMallLog($userId, $toUserId, $goodsId, $goodsName, $count, $price)
    {
        $data = [
            'user_id' => $userId,
            'to_user_id' => $toUserId,
            'goods_id' => $goodsId,
            'goods_name' => $goodsName,
            'count' => $count,
            'price' => $price
        ];
        return DB::table($this->logTable)->insertGetId($data);
    }

    /**
     * 道具商品-创建/更新/删除
     *
     * @param array $data 数据
     * @param string $type 操作类型 默认 insert=创建，update=更新
     * @param int $propMallId 道具商品ID
     * @return bool
     * */
    public function CUDPropMall($data = [], $type = 'insert', $propMallId = 0)
    {
        switch ($type) {
            case 'insert':
                $ret = DB::table($this->table)->insertGetId($data);
//                $propEventId = $ret;
                break;
            case 'update':
                $ret = DB::table($this->table)->where([$this->primaryKey => $propMallId])->update($data);
                break;
            case 'delete':
                $ret = DB::table($this->table)->where([$this->primaryKey => $propMallId])->delete();
                break;
            default:
                $ret = false;
                break;
        }

        return $ret;
    }
}
